#include <REGX52.H>
#include <stdlib.h>
#include "LCD1602.h"
#include "MatrixKey.h"
#include "Delay.h"
#include "AT24C02.h"
#include "DS18B20.h"
#include "Key.h"
#include "Timer0.h"
#include "Buzzer.h"
#include "MatrixLED.h"

unsigned char Flag=0,Flag_Directory=0,Flag_Show=0,Flag_Game_Show=0,Flag_Lock=0;
unsigned char Password_User[6]={0};
unsigned char Password_Set[6]={0};
unsigned Key_0=0,Key_1=0;
unsigned char j=0,k=0,m=0,i=0,n=0,p=0;
unsigned char count=0,count_wrong=0;
unsigned int locktime;
unsigned char Data_1[7]={0};
unsigned char Data_2[7]={0};
unsigned char Data_1_inverse[7]={0};
unsigned char Data_2_inverse[7]={0};
unsigned char d1=0,d2=0;
char Data_result[10]={0};
char Data_result_Temp[10]={0};
float T,TShow;
char TLow,THigh;


void Init_AT24C02()//部分EEPROM重置
{
    for(i=8;i<14;i++)
    {
        AT24C02_WriteByte(i,0);
        Delay(5);
    }
}


void LCD_Show_Directory()//目录显示
{
    if(Flag_Directory==0)
    {
        LCD_ShowString(1,1,"Directory       ");
        LCD_ShowString(2,1,"1.Calculator    ");
    }
    if(Flag_Directory==1)
    {
        LCD_ShowString(1,1,"2.              ");
        LCD_ShowString(2,1,"Combination Lock");
    }
    if(Flag_Directory==2)
    {
        LCD_ShowString(1,1,"3.              ");
        LCD_ShowString(2,1,"Small Game      ");
    }
    if(Flag_Directory==3)
    {
        LCD_ShowString(1,1,"4.              ");
        LCD_ShowString(2,1,"Thermometer     ");
    }
}

void LCD_Show_Calculator()//智能计算器显示
{
    if(AT24C02_ReadByte(9)==0)//初始化计算器
    {
        LCD_ShowString(1,1,"Calculator      ");
        LCD_ShowString(2,1,"                ");
        AT24C02_WriteByte(11,1);
        Delay(5);
        for(i=0;i<10;i++)
        {
            Data_result[i]=0;
            Data_result_Temp[i]=0;
        }
        for(i=0;i<7;i++)
        {
            Data_1[i]=0;
            Data_2[i]=0;
            Data_1_inverse[i]=0;
            Data_2_inverse[i]=0;
        }
        d1=0;
        d2=0;
        count=0;
        AT24C02_WriteByte(13,0);
        Delay(5);
        AT24C02_WriteByte(12,0);
        Delay(5);
        AT24C02_WriteByte(9,1);
        Delay(5);
        AT24C02_WriteByte(10,0);
        Delay(5);
    }
    if(AT24C02_ReadByte(9)==1)//显示算式
    {
        LCD_ShowString(1,1,"Calculator      ");
        for(i=0;i<d1;i++)//显示两个数
        {
            LCD_ShowNum(2,i+1,Data_1[i],1);
        }
        if(AT24C02_ReadByte(12)==1)// +
        {
            LCD_ShowChar(2,d1+1,'+');
        }
        if(AT24C02_ReadByte(12)==2)// -
        {
            LCD_ShowChar(2,d1+1,'-');
        }
        if(AT24C02_ReadByte(12)==3)// *
        {
            LCD_ShowChar(2,d1+1,'*');
        }
        if(AT24C02_ReadByte(12)==4)// /
        {
            LCD_ShowChar(2,d1+1,'/');
        }
        for(i=0;i<d2;i++)
        {
            LCD_ShowNum(2,d1+2+i,Data_2[i],1);
        }
    }
    if(AT24C02_ReadByte(9)==2)//显示答案
    {
        p=9;//p值需后续调整
        while(Data_result[p]==0)
        {
            p--;
        }
        LCD_ShowString(1,1,"Answer:         ");
        if(AT24C02_ReadByte(13))
        {
            LCD_ShowChar(1,8,'-');
            for(i=0;i<=p;i++)
            {
                LCD_ShowNum(1,i+9,Data_result[p-i],1);
            }
        }
        else
        {
            for(i=0;i<=p;i++)
            {
                LCD_ShowNum(1,i+8,Data_result[p-i],1);
            }
        }
        Delay(2000);
        AT24C02_WriteByte(9,0);
        Delay(5);
    }
    if(AT24C02_ReadByte(9)==3)//显示负数
    {
        LCD_ShowString(1,1,"Calculator      ");
        LCD_ShowChar(2,1,'-');
        for(i=0;i<d1;i++)//显示两个数
        {
            LCD_ShowNum(2,i+2,Data_1[i],1);
        }
        if(AT24C02_ReadByte(12)==1)// +
        {
            LCD_ShowChar(2,d1+2,'+');
        }
        if(AT24C02_ReadByte(12)==2)// -
        {
            LCD_ShowChar(2,d1+2,'-');
        }
        if(AT24C02_ReadByte(12)==3)// *
        {
            LCD_ShowChar(2,d1+2,'*');
        }
        if(AT24C02_ReadByte(12)==4)// /
        {
            LCD_ShowChar(2,d1+2,'/');
        }
        for(i=0;i<d2;i++)
        {
            LCD_ShowNum(2,d1+3+i,Data_2[i],1);
        }    
    }
}

void LCD_Show_Password()//密码锁显示
{
    if(Flag==0)//初始显示
    {
        LCD_ShowString(1,1,"Combination Lock");
        LCD_ShowString(2,1,"000000          ");
        for(i=0;i<6;i++)
        {
            Password_User[i]=0;
        }
        for(i=0;i<6;i++)
        {
            Password_Set[i]=AT24C02_ReadByte(i);//存进对应的EEPROM
            Delay(5);
        }
        Flag=1;
    }
    else if(Flag==1)//进入密码显示
    {
        LCD_ShowString(1,1,"Combination Lock");
        for(i=0;i<6;i++)
        {
            LCD_ShowNum(2,i+1,Password_User[i],1);
        }
    }
    else if(Flag==2)//进入正确显示
    {
        LCD_ShowString(1,1,"Combination Lock");
        LCD_ShowString(2,1,"Correct Password");
        Delay(1000);
        j=0;
        m=0;
        count=0;
        count_wrong=0;
        Flag=0;
    }
    else if(Flag==3)//进入错误显示
    {
        LCD_ShowString(1,1,"Combination Lock");
        LCD_ShowString(2,1,"Wrong Password");
        Delay(1000);
        count=0;
        j=0;
        m=0;
        Flag=0;
    }
    else if(Flag==4)//进入屏幕锁定显示
    {
        locktime=5;
        while(locktime)
        {
            LCD_ShowString(1,1,"Scream is LOCKED");
            LCD_ShowString(2,1,"Locktime:");
            LCD_ShowNum(2,10,locktime,1);
            LCD_ShowString(2,11,"s");
            Delay(1000);
            locktime--;
        }
        count=0;
        j=0;
        m=0;
        Flag=0;
    }
    else if(Flag==5)//进入修改密码显示————确认密码
    {
        LCD_ShowString(1,1,"Origin Password ");
        for(i=0;i<6;i++)
        {
            LCD_ShowNum(2,i+1,Password_User[i],1);
        }        

    }
    else if(Flag==6)//进入修改密码显示————修改密码
    {
        LCD_ShowString(1,1,"New Password    ");
        for(i=0;i<6;i++)
        {
            LCD_ShowNum(2,i+1,Password_Set[i],1);
        }
        j=0;
    }
    else if(Flag==7)//进入修改密码成功显示
    {
        LCD_ShowString(1,1,"The password has");
        LCD_ShowString(2,1,"been changed    ");
        Delay(1000);
        Flag=0;
        m=0;
        count=0;
    }
}


void LCD_Show_Game()//小游戏显示
{
    if(Flag_Game_Show==0)
    {
        LCD_ShowString(1,1,"Ranking         ");
        LCD_ShowString(2,1,"                ");//清空之前显示
        Flag_Game_Show=1;
    }
    if(Flag_Game_Show==1)
    {
        LCD_ShowString(1,1,"Ranking         ");
        LCD_ShowString(2,1,"Last Grade:");
        LCD_ShowNum(2,12,AT24C02_ReadByte(14),2);//显示分数       
        LCD_ShowString(2,14,"   ");//清空之前显示
    }
    if(Flag_Game_Show==2)
    {
        LCD_ShowString(1,1,"Ranking         ");
        LCD_ShowString(2,1,"1.");//显示位次
        LCD_ShowNum(2,3,AT24C02_ReadByte(15),2);//显示分数 
        LCD_ShowString(2,5,"                ");//清空之前显示
    }
    if(Flag_Game_Show==3)
    {
        LCD_ShowString(1,1,"Ranking         ");
        LCD_ShowString(2,1,"2.");//显示位次
        LCD_ShowNum(2,3,AT24C02_ReadByte(16),2);//显示分数 
        LCD_ShowString(2,5,"                ");//清空之前显示
    }
    if(Flag_Game_Show==4)
    {
        LCD_ShowString(1,1,"Ranking         ");
        LCD_ShowString(2,1,"3.");//显示位次
        LCD_ShowNum(2,3,AT24C02_ReadByte(17),2);//显示分数 
        LCD_ShowString(2,5,"                ");//清空之前显示
    }
}


void LCD_Show_Thermometer()//温度显示
{
	THigh=AT24C02_ReadByte(6);
	TLow=AT24C02_ReadByte(7);
    LCD_ShowString(1,1,"T:");
	LCD_ShowString(2,1,"TH:");
	LCD_ShowString(2,9,"TL:");
	LCD_ShowSignedNum(2,4,THigh,3);
    LCD_ShowChar(2,8,' ');
	LCD_ShowSignedNum(2,12,TLow,3);
	DS18B20_ConvertT();
	T=DS18B20_ReadT();
	if(T<0)
	{
		LCD_ShowChar(1,3,'-');
		TShow=-T;
	}
	else
	{
		LCD_ShowChar(1,3,'+');
		TShow=T;
	}
	LCD_ShowNum(1,4,TShow,3);
	LCD_ShowChar(1,7,'.');
	LCD_ShowNum(1,8,(unsigned long)(TShow*100)%100,2);
    if(T>THigh)
    {
        Buzzer_Time(100);
    }
}
void main()
{
    Init_AT24C02();
    DS18B20_ConvertT();
	Delay(1000);
	Timer0_Init();                                                                                                                                                   
    LCD_Init();
	while(1)
	{
        Key_1=Key();
        Key_0=MatrixKey();
        if(Key_0)
        {
            if(Flag_Show==0)//目录
            {
                if(Key_0==15)//菜单下一页
                {
                    Flag_Directory++;
                    Flag_Directory%=4;
                }
                if(Key_0==16)//菜单上一页
                {
                    Flag_Directory--;
                    if(Flag_Directory<0)
                    {
                        Flag_Directory=3;
                    }
                }
                if(Key_0==11)
                {
                    Flag_Show=Flag_Directory+1;
                    Key_0=0;
                }            
            }
            if(Flag_Show==1)//计算机
            {
                if(AT24C02_ReadByte(11)==1)//输入第一个数和符号
                {
                    if(Key_0>=1&&Key_0<=10)
                    {
                        Key_0%=10;
                        Data_1[d1]=Key_0;
                        d1++;
                    }
                    if(Key_0==11||Key_0==12||Key_0==15||Key_0==16)//11:+; 12:-; 15:*; 16:/;
                    {
                        if(Key_0==11)// +
                        {
                            AT24C02_WriteByte(12,1);
                            Delay(5);
                        }
                        if(Key_0==12)// -
                        {
                            AT24C02_WriteByte(12,2);
                            Delay(5);
                        }
                        if(Key_0==15)// *
                        {
                            AT24C02_WriteByte(12,3);
                            Delay(5);
                        }
                        if(Key_0==16)// /
                        {
                            AT24C02_WriteByte(12,4);
                            Delay(5);
                        }
                        AT24C02_WriteByte(11,2);
                        Delay(5);
                    }
                }
                if(AT24C02_ReadByte(11)==2)//输入第二个数
                {
                    if(Key_0>=1&&Key_0<=10)
                    {
                        Key_0%=10;
                        Data_2[d2]=Key_0;
                        d2++;
                    }
                    if(Key_0==13)
                    {
                        AT24C02_WriteByte(11,3);
                        Delay(5);
                        AT24C02_WriteByte(9,2);
                        Delay(5);
                    }
                }
                if(AT24C02_ReadByte(11)==3)//计算答案
                {
                    if(AT24C02_ReadByte(12)==1&&AT24C02_ReadByte(10)!=1)// +
                    {
                        for(i=0;i<d1;i++)
                        {
                            Data_1_inverse[i]=Data_1[d1-1-i];
                        }
                        for(i=0;i<d2;i++)
                        {
                            Data_2_inverse[i]=Data_2[d2-1-i];
                        }
                        for(i=0;i<d1||i<d2;i++)
                        {
                            Data_result[i]+=(Data_1_inverse[i]+Data_2_inverse[i]);
                            if(Data_result[i]>=10)
                            {
                                Data_result[i]%=10;
                                Data_result[i+1]++;
                            }
                        }

                    }
                    if(AT24C02_ReadByte(12)==2&&AT24C02_ReadByte(10)!=1)// -
                    {
                        for(i=0;i<d1;i++)
                        {
                            Data_1_inverse[i]=Data_1[d1-1-i];
                        }
                        for(i=0;i<d2;i++)
                        {
                            Data_2_inverse[i]=Data_2[d2-1-i];
                        }
                        if(d2>d1)
                        {
                            AT24C02_WriteByte(13,1);//记得置零
                            Delay(5);
                            for(i=0;i<d1||i<d2;i++)
                            {
                                Data_result[i]+=(Data_2_inverse[i]-Data_1_inverse[i]);
                                if(Data_result[i]<0)
                                {
                                    Data_result[i]+=10;
                                    Data_result[i+1]--;
                                }
                            }
                        }
                        else
                        {
                            for(i=0;i<d1||i<d2;i++)
                            {
                                Data_result[i]+=(Data_1_inverse[i]-Data_2_inverse[i]);
                                if(Data_result[i]<0)
                                {
                                    Data_result[i]+=10;
                                    Data_result[i+1]--;
                                }
                            }
                            if(Data_result[d1]<0||Data_result[d2]<0)
                            {
                                AT24C02_WriteByte(13,1);//记得置零 
                                Delay(5);                                
                                for(i=0;i<=d1||i<=d2;i++)
                                {
                                    Data_result[i]=0;
                                }
                                for(i=0;i<d1||i<d2;i++)
                                {
                                    Data_result[i]+=(Data_2_inverse[i]-Data_1_inverse[i]);
                                    if(Data_result[i]<0)
                                    {
                                        Data_result[i]+=10;
                                        Data_result[i+1]--;
                                    }
                                }
                            }
                            
                            
                            
                        }
                    }
                    if(AT24C02_ReadByte(12)==3)// *
                    {
                        for(i=0;i<d1;i++)
                        {
                            Data_1_inverse[i]=Data_1[d1-1-i];
                        }
                        for(i=0;i<d2;i++)
                        {
                            Data_2_inverse[i]=Data_2[d2-1-i];
                        }
                        for(i=0;i<d1;i++)
                        {
                            for(n=0;n<d2;n++)
                            {
                                Data_result[i+n]+=(Data_1_inverse[i]*Data_2_inverse[n]);
                            }
                            if(Data_result[i+n]>=10)
                            {
                                Data_result[i+n+1]+=(Data_result[i+n]/10);
                                Data_result[i+n]%=10;
                            }
                        }
                    }
                    if(AT24C02_ReadByte(12)==4)// /
                    {
                        for(i=0;i<d1;i++)//翻转第一个数
                        {
                            Data_1_inverse[i]=Data_1[d1-1-i];
                        }
                        for(i=0;i<d2;i++)//翻转第二个数
                        {
                            Data_2_inverse[i]=Data_2[d2-1-i];
                        }
                        while(1)
                        {
                            for(i=0;i<d1;i++)
                            {
                                Data_result_Temp[i]+=(Data_1_inverse[i]-Data_2_inverse[i]);
                                if(Data_result_Temp[i]<0)
                                {
                                    Data_result_Temp[i]+=10;
                                    Data_result_Temp[i+1]--;
                                }
                            }
                            count=0;
                            for(i=0;i<d1;i++)
                            {
                                if(Data_result_Temp[i]==0)
                                {
                                    count++;
                                }
                            }
                            if(count==d1)
                            {
                                Data_result[0]++;
                                for(i=0;i<d1;i++)
                                {
                                    if(Data_result[i]>=10)
                                    {
                                        Data_result[i+1]++;
                                        Data_result[i]%=10;
                                    }
                                }
                                count=0;
                                break;
                            }
                            if(Data_result_Temp[d2]<0)
                            {
                                break;
                            }
                            Data_result[0]++;
                            for(i=0;i<d1;i++)
                            {
                                if(Data_result[i]>=10)
                                {
                                    Data_result[i+1]++;
                                    Data_result[i]%=10;
                                }
                            }
                            for(i=0;i<d1;i++)
                            {
                                Data_1_inverse[i]=Data_result_Temp[i];
                            }
                            for(i=0;i<=d1;i++)
                            {
                                Data_result_Temp[i]=0;
                            }
                        }
                    
                    }
                    if(AT24C02_ReadByte(12)==1&&AT24C02_ReadByte(10)==1)//负数加正数
                    {
                        for(i=0;i<d1;i++)
                        {
                            Data_1_inverse[i]=Data_1[d1-1-i];
                        }
                        for(i=0;i<d2;i++)
                        {
                            Data_2_inverse[i]=Data_2[d2-1-i];
                        }
                        if(d1>d2)
                        {
                            AT24C02_WriteByte(13,1);//记得置零
                            Delay(5);
                            for(i=0;i<d1||i<d2;i++)
                            {
                                Data_result[i]+=(Data_1_inverse[i]-Data_2_inverse[i]);
                                if(Data_result[i]<0)
                                {
                                    Data_result[i]+=10;
                                    Data_result[i+1]--;
                                }
                            }
                        }
                        else
                        {
                            for(i=0;i<d1||i<d2;i++)
                            {
                                Data_result[i]+=(Data_2_inverse[i]-Data_1_inverse[i]);
                                if(Data_result[i]<0)
                                {
                                    Data_result[i]+=10;
                                    Data_result[i+1]--;
                                }
                            }
                            if(Data_result[d1]<0||Data_result[d2]<0)
                            {
                                AT24C02_WriteByte(13,1);//记得置零 
                                Delay(5);                                
                                for(i=0;i<=d1||i<=d2;i++)
                                {
                                    Data_result[i]=0;
                                }
                                for(i=0;i<d1||i<d2;i++)
                                {
                                    Data_result[i]+=(Data_1_inverse[i]-Data_2_inverse[i]);
                                    if(Data_result[i]<0)
                                    {
                                        Data_result[i]+=10;
                                        Data_result[i+1]--;
                                    }
                                }
                            }
                        }
                        
                    }
                    if(AT24C02_ReadByte(12)==2&&AT24C02_ReadByte(10)==1)//负数减负数
                    {
                        for(i=0;i<d1;i++)
                        {
                            Data_1_inverse[i]=Data_1[d1-1-i];
                        }
                        for(i=0;i<d2;i++)
                        {
                            Data_2_inverse[i]=Data_2[d2-1-i];
                        }
                        for(i=0;i<d1||i<d2;i++)
                        {
                            Data_result[i]+=(Data_1_inverse[i]+Data_2_inverse[i]);
                            if(Data_result[i]>=10)
                            {
                                Data_result[i]%=10;
                                Data_result[i+1]++;
                            }
                        }
                        AT24C02_WriteByte(13,1);
                        Delay(5);
                    }
                }
                if(Key_0==14)//回到目录
                {
                    Flag_Show=0;
                    count=0;
                    AT24C02_WriteByte(11,0);
                    Delay(5);
                    AT24C02_WriteByte(9,0);
                    Delay(5);
                    AT24C02_WriteByte(11,0);
                    Delay(5);
                    d1=0;
                    d2=0;
                    
                }
                
            }
            if(Flag_Show==2)//密码锁
            {
//                AT24C02_WriteByte(0,1);//用于强制改密码
//                Delay(5);
//                AT24C02_WriteByte(1,3);
//                Delay(5);
//                AT24C02_WriteByte(2,5);
//                Delay(5);
//                AT24C02_WriteByte(3,7);
//                Delay(5);
//                AT24C02_WriteByte(4,9);
//                Delay(5);
//                AT24C02_WriteByte(5,0);
//                Delay(5);
                if(Key_0>=1&&Key_0<=10)//用户输入密码。
                {
                    if(Flag==6)//修改密码
                    {
                        if(m<=5)
                        {
                            Key_0%=10;
                            Password_Set[m]=Key_0;
                            AT24C02_WriteByte(m,Key_0);
                            Delay(5);
                            m++;
                        }
                    }
                    else
                    {
                        if(j<=5)
                        {
                            Key_0%=10;
                            Password_User[j]=Key_0;
                            j++;
                        }
                    }
                }
                if(Key_0==11)//显示密码正误
                {
                    unsigned char s;
                    for(s=0;s<6;s++)
                    {
                        if(Password_User[s]==Password_Set[s])
                        {count++;}
                    }
                    if(AT24C02_ReadByte(8)==1)
                    {
                        Flag=7;
                        AT24C02_WriteByte(8,0);
                        Delay(5);
                    }
                    if(count==6)
                    {
                        if(Flag==5)
                        {
                            AT24C02_WriteByte(8,1);
                            Delay(5);
                            count_wrong=0;
                            for(i=0;i<6;i++)
                            {
                                Password_Set[i]=0;
                            }
                            Flag=6;
                        }
                        else if(Flag==1)
                        {
                            Flag=2;
                            count_wrong=0;
                            Flag_Lock=0;
                        }
                    }
                    else
                    {
                        if(Flag==5)
                        {
                            AT24C02_WriteByte(8,0);
                            Delay(5);
                            Flag=3;
                            count_wrong++;
                        }
                        else if(Flag==1)
                        {
                            Flag=3;
                            count_wrong++;
                        }
                    }
                        if(count_wrong>=3)
                        {
                            Flag=4;
                        }
                }
                if(Key_0==12)//清除前一位输入
                {
                    if(j>=1)
                    {
                        j--;
                        Password_User[j]=0;
                    }
                }
                if(Flag_Lock==0)
                {
                    if(Key_0==13)//进入修改密码
                    {
                        Flag=5;
                    }
                    if(Key_0==14)//回到目录
                    {
                        Flag_Show=0;
                        j=0;
                        m=0;
                        count=0;
                        count_wrong=0;
                        Flag=0;
                    }
                }
            }
            if(Flag_Show==3)//贪吃蛇小游戏
            {
//                for(i=14;i<=17;i++)//重置排行榜
//                {
//                    AT24C02_WriteByte(i,0);
//                    Delay(5);
//                }
                if(Key_0==15)//向下翻页
                {
                    Flag_Game_Show++;
                    Flag_Game_Show%=5;
                }
                if(Key_0==16)//向上翻页
                {
                    Flag_Game_Show--;
                    if(Flag_Game_Show<0)
                    {
                        Flag_Game_Show=4;
                    }
                }
                if(Key_0==14)//退回菜单
                {
                    Flag_Show=0;
                }
            }
        }            
        if(Flag_Show==4)//温度计
        {
            LCD_Show_Thermometer();
            if(Key_1==1)//调高最高温度
            {
                THigh++;
                if(THigh>125){THigh=125;}
            }
            if(Key_1==2)//调低最搞温度
            {
                THigh--;
                if(THigh<=TLow){THigh++;}
            }
            if(Key_1==3)//调高最低温度
            {
                TLow++;
                if(TLow>=THigh){TLow--;}
            }
            if(Key_1==4)//调低最低温度
            {
                TLow--;
                if(TLow<-55){TLow=-55;}
            }
            AT24C02_WriteByte(6,THigh);	
            Delay(5);
            AT24C02_WriteByte(7,TLow);
            Delay(5);
            if(Key_0==14)//回到菜单
            {
                Flag_Show=0;
            }
        }
        if(Flag_Show==0)//显示菜单
        {
            LCD_Show_Directory();            
        }
        if(Flag_Show==1)//显示计算机
        {
            if(Key_1==1)
            {
                AT24C02_WriteByte(9,3);
                Delay(5);
                AT24C02_WriteByte(10,1);
                Delay(5);
            }
            LCD_Show_Calculator();
        }
        if(Flag_Show==2)//显示密码锁
        {
            LCD_Show_Password();            
        }
        if(Flag_Show==3)//显示小游戏
        {
            LCD_Show_Game();
        }
	}
}

void Timer0_Routine() interrupt 1
{
	static unsigned char T0Count;
    static unsigned int T0Count1;
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	T0Count++;
	if(T0Count>=20)
	{
		T0Count=0;
		Key_Loop();	//每20ms调用一次按键函数
	}
    if(Key_0==0&&Key_1==0)
    {
        T0Count1++;
    }
    else
    {
        T0Count1=0;
    }
    if(T0Count1>=50000)//无操作五秒进入密码锁且无法退出
    {
        T0Count1=0;
        Flag_Show=2;
        Flag_Lock=1;
    }
}


/*AT24C02占用：
0~5：密码锁强制改密码和存储修改后密码
6~7：存储温度计的高温低温
8  :Flag_Change
9  :Flag_Calculator
10 :负数计算标志位
11 :Flag_Input
12 :Flag_Operation
13 :Flag_PN
14 :存储小游戏上一局的分数
15~17:存储小游戏前三排名的分数
18 :第二个数为负数标志位
*/


